import { ElMessage } from 'element-plus'
import { deleteReq,multiDeleteReq } from '@/api/oss'

import { i18n } from '@/lang'
const { t } = i18n.global

const single = ref(true)
const multiple = ref(true)
/*table 列表*/
const ids = ref([])
const totalNum = ref(0)
const loading = ref(false)
const ossList = ref([])
const showSearch = ref(true)
const refDownEditModal = ref()
const refElTable = ref()
const refExport = ref()
export const handleImport = () => {
  refExport.value.showModal()
}


const tableHeadColumns = ref([
  { prop: 'filename', label: ('downModel.filename'), minWidth: 150, isTemplate: false, align: 'center', showColumn: true },
  { prop: 'file_type', label: ('downModel.file_type'), minWidth: 150, isTemplate: false, align: 'center', showColumn: true },
  { prop: 'source', label: ('downModel.source'), minWidth: 150, isTemplate: false, align: 'center', showColumn: true },
  { prop: 'dowanload_at', label: ('downModel.dowanload_at'), minWidth: 150, isTemplate: false, align: 'center', showColumn: true },
  { prop: 'download_times', label: ('downModel.download_times'), minWidth: 150, isTemplate: false, align: 'center', showColumn: true },
])

export const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.ossId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
export const colChange = (heardColsArr) => {
  tableHeadColumns.value = heardColsArr
}

export const handleAdd = () => {
  refDownEditModal.value.showModal({})
}

export const removeEmptyKey = (data) => {
  return Object.keys(data)
    .filter((key) => data[key] !== null && data[key] !== undefined && data[key] !== '')
    .reduce((acc, key) => ({ ...acc, [key]: data[key] }), {})
}

watch(
  () => tableHeadColumns,
  () => {
    if (refElTable) {
      nextTick(refElTable.value.doLayout)
    }
  },
  { deep: true }
)

export const currentHook = (queryParams, getList) => {

  const handleDelete = (row) => {
    const ossIds = row.ossId || ids.value
    elConfirm('确认', `是否确认删除用户编号为"${ossIds}"的数据项`)
      .then(() => {
        return deleteReq(ossIds)
      })
      .then(() => {
        ElMessage({ message: '删除成功', type: 'success' })
        getList()
      })
  }




  const handleDown = (row) => {
    const ossIds = row.ossId || ids.value
    elConfirm('确认', `是否确认删除用户编号为"${ossIds}"的数据项`)
      .then(() => {
        return deleteReq(ossIds)
      })
      .then(() => {
        ElMessage({ message: '删除成功', type: 'success' })
        getList()
      })
  }

  const handleDeleteMul = () => {
    const ossIds = ids.value
    elConfirm('确认', `是否确认删除用户编号为"${ossIds}"的数据项`)
      .then(() => {
        return multiDeleteReq(ossIds)
      })
      .then(() => {
        ElMessage({ message: '删除成功', type: 'success' })
        getList()
      })
  }

  return {
    handleDeleteMul,
    refDownEditModal,
    handleDelete,
    handleDown,
    refElTable,
    refExport,
    multiple,
    ids,
    single,
    totalNum,
    loading,
    ossList,
    showSearch,
    tableHeadColumns
    // handleExport
  }
}
